//
//  CCAListViewModel.m
//  Activity
//
//  Created by its on 15/5/14.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCAListViewModel.h"
#import "CCAcitivityModel.h"
#import "CCLocation.h"
#import <LinqToObjectiveC/NSArray+LinqExtensions.h>

@interface CCAListViewModel()
@property (nonatomic, strong) CCAcitivityModel *model;
@property (nonatomic, strong) NSNumber *lastOrder;
@property (nonatomic, strong) NSNumber *headOrder;
@property (nonatomic, strong) CCLocation *locationService;
@end

@implementation CCAListViewModel
- (id) initWithModel:(CCAcitivityModel *)model {
  if ( self = [super init] ) {
    _model = model;
    _viewTitleString = @"活动";
    _activityArray = [NSMutableArray array];
    _city = @"正在定位";
  }
  return self;
}

- (RACSignal *) getActivity {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    return [[_model getActivity:_city] subscribeNext:^(id x) {
      if ([x isKindOfClass:[CCError class]]) {
        [subscriber sendNext:x];
      } else {
        self.activityArray = [self.activityArray linq_concat:x];
        CCAcitivity *activity = [x lastObject];
        self.lastOrder = activity.index;
        self.headOrder = [(CCAcitivity *)x[0] index];
        [subscriber sendNext:ERROR(2002)];
      }
    }];
  }];
}

- (RACSignal *)  getFoot {
  if (self.lastOrder == nil) {
    return [self getActivity];
  } else {
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
      [[self.model getFoot:self.lastOrder limit:10 city:(self.location? self.city : @"北京市")] subscribeNext:^(id x) {
        if ([x isKindOfClass: [CCError class]]) {
          [subscriber sendNext: x];
        } else {
          self.activityArray = [self.activityArray linq_concat:x];
          CCAcitivity *activity = [x lastObject];
          self.lastOrder = activity.index;
          [subscriber sendNext: ERROR(2002)];
        }
        [subscriber sendCompleted];
      }];
      return [RACDisposable disposableWithBlock:^{
        [subscriber sendCompleted];
      }];
    }];
  }
}

- (RACSignal *) getHeader {
  if (self.lastOrder == nil) {
    return [self getActivity];
  } else {
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
      [[self.model getHeader:self.headOrder  city:(self.location? self.city : @"北京市")] subscribeNext:^(id x) {
        if ([x isKindOfClass: [CCError class]]) {
          [subscriber sendNext: x];
        } else {
          CCAcitivity *activity = [x objectAtIndex:0];
          self.headOrder = activity.index;
          self.activityArray = [x linq_concat:self.activityArray];
          [subscriber sendNext: ERROR(2002)];
        }
        [subscriber sendCompleted];
      }];
      return [RACDisposable disposableWithBlock:^{
        [subscriber sendCompleted];
      }];
    }];
  }
}

- (RACSignal *) locationCity {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    self.locationService = [[CCLocation alloc] init];
    @weakify(self);
    [self.locationService startLocaion:^(NSDictionary *result, NSError *error) {
      @strongify(self);
      if (error) {
        self.city = @"定位失败";
        [subscriber sendError:error];
      } else {
        self.location = YES;
        self.city = [result objectForKey:@"city"];
        [subscriber sendNext: self.city];
        [subscriber sendCompleted];
      }
    }];
    return [RACDisposable disposableWithBlock:^{
      [subscriber sendCompleted];
    }];
  }];
}
@end
